package com.techsoul.module.goods.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.CategoryVo;
import com.techsoul.module.goods.pojo.param.CategorySaveParam;
import com.techsoul.module.goods.pojo.param.CategorySearchParam;
import com.techsoul.module.goods.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 分类 Controller
 *
 * @author liuen
 * @date 2020-12-14
 */
@Api(tags = "分类")
@RestController
@RequestMapping("/goods/category")
public class CategoryController {

    @Autowired
    private Result result;
    
    @Autowired
    private ICategoryService service;

    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('goods:category:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody CategorySaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('goods:category:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('goods:category:update')")
    @PutMapping
    public Result update(@Valid @RequestBody CategorySaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("更新树形表信息")
    @PreAuthorize("hasAnyAuthority('goods:category:update')")
    @PutMapping("updateAll")
    public Result updateAll() {
        this.service.updateAll();
        return result.success();
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('goods:category:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('goods:category:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody CategorySearchParam params) {
        List<CategoryVo> all = this.service.findAll(params);
        return result.success(all);
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('goods:category:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody CategorySearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("树形结构数据")
    @PreAuthorize("hasAnyAuthority('goods:category:search')")
    @PostMapping("/search/tree")
    public Result findTree(@RequestBody CategorySearchParam params) {
        return result.success(this.service.findTree(params));
    }

    @ApiOperation("树形结构选择器数据")
    @PreAuthorize("hasAnyAuthority('goods:category:search')")
    @PostMapping("/search/treeSelect")
    public Result findTreeSelect(@RequestBody CategorySearchParam params) {
        return result.success(this.service.findTreeSelect(params));
    }

}
